package com.sgffsg.learn.javalib.thread;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lmk
 * @date: 2024/3/31
 * @desc: 生产者消费者模型wait/notify实现
 */
public class ProducerConsumerModel2 {

    private List<String> products = new ArrayList<String>();
    private Object lock = new Object();
    private static final int MAX = 3;
    private int number = 0;

    public static void main(String[] args) {
        ProducerConsumerModel2 pcm = new ProducerConsumerModel2();
        pcm.startTest();
    }

    public void startTest() {
        for (int i = 0; i < 3; i++) {
            Producer producer = new Producer("Producer-"+i);
            producer.start();
            Consumer consumer = new Consumer("Consumer-"+i);
            consumer.start();
        }
    }

    class Producer extends Thread {
        Producer(String name) {
            setName(name);
        }

        @Override
        public void run() {
            super.run();
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock) {
                    while (products.size() >= MAX) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "-库房已满，不在生产,等待中");
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    String product = "iphone编号：" + number++;
                    products.add(product);
                    System.out.println(Thread.currentThread().getName() + "生产了产品-" + product);
                    lock.notify();

                }
            }
            System.out.println(Thread.currentThread().getName()+"-本轮生产完成");
        }
    }

    class Consumer extends Thread {
        Consumer(String name) {
            setName(name);
        }

        @Override
        public void run() {
            super.run();
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock) {
                    while (products.size() == 0){
                        System.out.println(Thread.currentThread().getName()+"-没货卖个锤子！等待");
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    String product = products.get(0);
                    products.remove(0);
                    System.out.println(Thread.currentThread().getName()+"购买了产品-" + product);
                    lock.notify();
                }
            }
            System.out.println(Thread.currentThread().getName()+"-本轮购买完成");
        }
    }
}
